package com.zmn.oms.task.job.third.YouShu;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.third.youshu.YouShuConsts;
import com.zmn.oms.third.youshu.service.YouShuService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：腾讯有数接口上报定时任务
 *
 * @author ligang
 * @since 2022/3/23 17:23
 */
@LTS
@Slf4j
public class YouShuOrderJob {
    @Resource
    private YouShuService youShuService;

    @Resource
    private TaskManager taskManager;

    @PostConstruct
    void initJob() {
        // 任务：每天 3:00 前完成前一天的数据上报
        try {
            Job job = new Job();
            job.setTaskId(TaskConsts.TASK_ID_YOUSHU_PUSH_SUM_ORDER);
            job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_YOUSHU_PUSH_SUM_ORDER);
            job.setCronExpression("0 0 3 * * ?");
            job.setReplaceOnExist(true);
            ResultDTO result = taskManager.submitTask(job);
            if (!result.getStatusBool()) {
                log.error("【{}】汇总接口上报 job submit fail...", YouShuConsts.CHANNEL_NAME);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 腾讯有数汇总接口上报
     *
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_YOUSHU_PUSH_SUM_ORDER)
    public Result runSumOrdersJob(Job job) {
        try {
            youShuService.sendSumOrder();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        log.info("【{}】汇总接口上报任务执行成功 ！", YouShuConsts.CHANNEL_NAME);
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
